import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';

export default {
  name: 'payment',
  components: {},
  data () {
    return {
      appKey: '',
      appId:'',
      requestSecret:'',
      content: {
        id:'',
        name:''
      },
      paySignObj: '',
      openid:'',
      studentId:'',
      studentName:'',
      wx_pay_show:false,
      xa_pay_show: false,
      siteId:'',
      orderInfo: {
        product_id:'',
        product_name:'',
        count: '',
        price: ''
      }
    }
  },
  mounted () {
    this.content.id = this.$route.query.reserveNo
    this.content.name = this.$route.query.name
    this.content.totalFees = this.$route.query.totalFees
    this.content.area = this.$route.query.area
    this.content.classNo = this.$route.query.classNo
    this.content.frontStatusName = this.$route.query.frontStatusName
    this.content.year = this.$route.query.year
    this.content.term = this.$route.query.term
    this.content.spelName = this.$route.query.spelName
    this.content.degree = this.$route.query.degree
    this.orderInfo.product_id = this.content.id
    this.orderInfo.product_name = '微信' + this.content.id
    this.orderInfo.count = 1
    this.orderInfo.price = this.content.totalFees
    this.openid = sessionStorage.getItem("openId")
    this.studentId = sessionStorage.getItem("studentId")
    this.siteId = sessionStorage.getItem("siteId")
    this.studentName = this.$route.query.studentName
    this.appId = sessionStorage.getItem("appId")
    let payOpen = sessionStorage.getItem("payOpen")
    //alert(payOpen)
    if (this.appId == 'wx51945bc6cb8cb58a') {
      this.xa_pay_show = true
    } else {
      if (payOpen.indexOf("1") != -1) {
        this.wx_pay_show = true
      }
    }
    http.get(CONSTANT.GETAPPKEY + '?appId=' + this.appId, {}, (res) => {
      this.appKey = res.appKey
    })
    //this.orderInfo.product_id = '1'
    //this.orderInfo.product_name = '语言表演'
    //this.orderInfo.count = 1
    //this.orderInfo.price = '100'
  },
  methods: {
    comeBack () {
      window.history.go(-1)
    },
    xapay () {
      if (this.orderInfo.price == '0') {
        alert('提示：0元免费请在窗口处理。谢谢！')
        return
      }
      var _this = this
      http.get(CONSTANT.PAYMENT_API + '/refresh_key?appKey=' + _this.appKey, {}, (res) => {
        _this.requestSecret = res.obj.requestSecret
      // alert(_this.requestSecret)
      // alert(JSON.stringify( _this.orderInfo))
      // console.log(encodeURIComponent(encodeURIComponent(CONSTANT.PAYMENT_API + "/payment?appKey=" + _this.appKey + "&requestSecret=" + _this.requestSecret + "&orderInfo=" + JSON.stringify( _this.orderInfo) + "&type=" + _this.type)))
      //let orderInfo = encodeURIComponent(JSON.stringify( _this.orderInfo))
      // location.href = "http://etzx.hztangong.com" + "/payment?appKey=" + _this.appKey + "&requestSecret=" + _this.requestSecret + "&orderInfo=" + orderInfo + "&type=" + _this.type +"&openid=" + sessionStorage.getItem("openId")
      _this.xaConfig();
    })
    },
    wxpay () {
      if (this.orderInfo.price == '0') {
          alert('提示：0元免费请在窗口处理。谢谢！')
          return
      }
      var _this = this
      http.get(CONSTANT.PAYMENT_API + '/refresh_key?appKey=' + _this.appKey, {}, (res) => {
        _this.requestSecret = res.obj.requestSecret
        // alert(_this.requestSecret)
        // alert(JSON.stringify( _this.orderInfo))
        // console.log(encodeURIComponent(encodeURIComponent(CONSTANT.PAYMENT_API + "/payment?appKey=" + _this.appKey + "&requestSecret=" + _this.requestSecret + "&orderInfo=" + JSON.stringify( _this.orderInfo) + "&type=" + _this.type)))
        //let orderInfo = encodeURIComponent(JSON.stringify( _this.orderInfo))
       // location.href = "http://etzx.hztangong.com" + "/payment?appKey=" + _this.appKey + "&requestSecret=" + _this.requestSecret + "&orderInfo=" + orderInfo + "&type=" + _this.type +"&openid=" + sessionStorage.getItem("openId")
      _this.weixinConfig();
      })
    },
    weixinConfig () {
      let thisPageUrl = window.location.href.split('#')[0]
      // console.log(thisPageUrl)
     // console.log('微信授权url****' + thisPageUrl)
      //alert(CONSTANT.PAYMENT_API+"/wxConifg")
      thisPageUrl = 'http://qsng.hztangong.com/wx/index.html'
      http.post(CONSTANT.PAYMENT_API+"/wxConfig", {'url': thisPageUrl,'appId':this.appId}, (res) => {
        if (res.code == 200) {
        console.log('config参数已返回')
        console.log(res)
        this.wx.config({
          "debug": false,
          "appId": res.obj.appid,
          "timestamp": res.obj.timestamp,
          "nonceStr": res.obj.nonceStr,
          "signature": res.obj.signature,
          "jsApiList": ['chooseWXPay']
        })
        //console.log('调用微信支付')
        //console.log(this.requestSecret)
        //console.log(this.product_id)
        var callBackUrl = 'http://apiwx.qsng.cn/app/wxpay/wxcallback.do?'
        callBackUrl += 'siteId=' + this.siteId + '&appKey=' + this.appKey + '&'
        http.post(CONSTANT.PAYMENT_API+"/api", {'appKey': this.appKey, 'requestSecret': this.requestSecret, 'type': '11',openid: this.openid,siteId: this.siteId,studentId: this.studentId, callBackUrl: callBackUrl, 'orderInfo': '{"count": 1,"product_id":"' + this.orderInfo.product_id + '", "product_name": "' + this.orderInfo.product_name + '","product_detail": "' + this.orderInfo.product_name + '","price":' + this.orderInfo.price+ '}'}, (res) => {
          //console.log(res)
        if (res.code == 200) {
          this.paySignObj = res.obj
          this.callPay()
        } else {
          alert('提交订单失败！')
        }
      })
      }
    })
    },
    xaConfig () {
      var callBackUrl = 'http://apiwx.qsng.cn/app/wxpay/wxcallback.do?'
      callBackUrl += 'siteId=' + this.siteId + '&appKey=' + this.appKey + '&'
      http.post(CONSTANT.PAYMENT_API+"/api", {'appKey': this.appKey, 'requestSecret': this.requestSecret, 'type': '41',openid: this.openid,siteId: this.siteId,studentId: this.studentId, callBackUrl: callBackUrl, 'orderInfo': '{"count": 1,"product_id":"' + this.orderInfo.product_id + '", "product_name": "' + this.orderInfo.product_name + '","product_detail": "' + this.orderInfo.product_name + '","price":' + this.orderInfo.price+ '}'}, (res) => {
        console.log(res)
        if (res.code == 200) {
          location.href = res.obj.pat
      } else {
        alert('提交订单失败！')
      }
      })
    },
    callPay () {
      var _this = this
      _this.wx.chooseWXPay({
        timestamp: _this.paySignObj.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
        nonceStr: _this.paySignObj.nonceStr, // 支付签名随机串，不长于 32 位
        package: _this.paySignObj.packageStr, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
        signType: "MD5", // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
        paySign: _this.paySignObj.paySign, // 支付签名
        success: function (res) {
          // 支付成功后的回调函数
          // window.history.go(-1)
        /*  alert(_this.siteId='---'+_this.appKey)
          http.get(CONSTANT.NOTIFYAPI, {
            siteId:_this.siteId,
            appKey:_this.appKey,
            id:_this.paySignObj.id,
            payType:'wx',
            product_id:_this.content.id
          }, (res) => {
            alert(JSON.stringify(res))
            if (res.code == 200) {

            }
          })*/
          _this.$router.push({path: '/mine'})
         // window.location.href = 'http://www.qqchou.org/a/wxinterface/wxOAuth'
        },
        fail: function(res){// 支付失败回调函数
          alert('fail, 支付失败请重新下单')
         // window.location.href = 'http://www.qqchou.org/a/wxinterface/wxOAuth?category=3&event_id=' + this.event_id
        },
        cencel: function () {
          alert('cancel, 支付失败请重新下单')
         // window.location.href = 'http://www.qqchou.org/a/wxinterface/wxOAuth?category=3&event_id=' + this.event_id
        }
      })
      // wx.error(function(res){
      // 	alert('error, 支付失败请重新下单')
      // 	alert(res.err_msg);
      // 	window.location.href = 'http://www.qqchou.org/a/wxinterface/wxOAuth?category=3&event_id=' + this.event_id
      // 	});
    },
    alipay () {

    }
  }
}
